﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace VeinServerCsharp
{
    /// <summary>
    ///版本号：  V1.0
    ///创建人：  奇剑藏锋
    ///电子邮箱：xiefengdaxia123@163.com
    ///创建时间：2017-12-29 11:20:16
    ///描述：消息处理类，用于处理接和发客户端消息
    /// </summary>
    public class MessageHandle
    {
        private Socket socket = null;

        /// <summary>
        /// 获取实例对象
        /// </summary>
        /// <param name="socket">套接字</param>
        /// <returns>返回消息对象</returns>
        public static MessageHandle getInstance(Socket socket)
        {
            MessageHandle mhObj = new MessageHandle();
            mhObj.socket = socket;
            return mhObj;
        }

        /// <summary>
        /// 处理客户端发来的socket消息
        /// </summary>
        /// <param name="cmd">客户端命令</param>
        /// <param name="recvBuf">接到的byte数组</param>
        /// <param name="datalen">数组长度</param>
        public void RecMsgHandle(int cmd, byte[] recvBuf, int datalen)
        {

            //校验和正确
            switch (cmd)
            {
                default:
                    break;
                case 0x01:
                    //心跳包
                    break;
                case 0x0F:
                    {
                        try
                        {
                            Thread.Sleep(100);
                        }
                        catch (Exception e)
                        {

                        }
                        //byte[] bData = new byte[16];
                        //bData[0] = 30;//谢谢
                        //socketServer.Send(BuildPack((byte)0x0A, bData, 1));
                        break;
                        byte[] bData = new byte[16];
                        bData = System.Text.Encoding.Default.GetBytes("佛盛龙软件服务端");
                        socket.Send(BuildPack((byte)0x0D, bData, bData.Length));
                        break;
                    }
            }
        }
        /// <summary>
        /// 组装包，待发给客户端
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="bData"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public byte[] BuildPack(byte cmd, byte[] bData, int len)
        {
            int PackSize = len + 8;
            if (PackSize < 24) PackSize = 24;

            byte[] PackBuf = new byte[PackSize];
            for (int i = 0; i < PackSize; i++)
            {
                PackBuf[i] = 0;
            }

            PackBuf[0] = (byte)0xDD;
            PackBuf[1] = (byte)0xCC;
            PackBuf[2] = (byte)0; //bAddress
            PackBuf[3] = (byte)cmd;
            PackBuf[4] = (byte)(len & 0xff);
            PackBuf[5] = (byte)((len >> 8) & 0xff);
            for (int i = 0; i < len; i++)
            {
                PackBuf[i + 6] = bData[i];
            }
            int checknum = CheckSum(PackBuf, PackSize - 2);
            PackBuf[len + 6] = (byte)(checknum & 0xff);
            PackBuf[len + 7] = (byte)(((checknum & 0xFFFF) >> 8) & 0xff);
            return PackBuf;
        }
        /// <summary>
        /// 校验和
        /// </summary>
        /// <param name="buf"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static int CheckSum(byte[] buf, int len)
        {
            int Sum = 0;
            int i;

            for (i = 0; i < len; i++)
            {
                Sum += buf[i] & 0xff;
            }
            return Sum & 0xffff;
        }

    }
}
